//
//  LBCoursePackageVC.m
//  V-Learn
//
//  Created by Apple on 2019/1/3.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "LBCoursePackageVC.h"
#import "LBSaomangCell.h"
#import "LBCoureseDetailVC.h"
#import "LBCoureseListModel.h"
#import "CourseDetailsViewController.h"
#import "LBTabBarViewController.h"
@interface LBCoursePackageVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *coureseLists;
@end

@implementation LBCoursePackageVC
{
    NSInteger  _page;
    NSInteger  _currentCount;
    NSInteger  _pageIndex;
}
- (NSMutableArray *)coureseLists{
    if (!_coureseLists) {
        _coureseLists = [NSMutableArray array];
    }
    return _coureseLists;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, ScreenW, ScreenH-LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"课程包";
    [self.view addSubview:self.tableView];

    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    //下拉刷新
    [self downRefreshData];
}
- (void)downRefreshData{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadDownNewDataSecond];
    }];
    
    //2.上拉
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadUpNewDataSecond)];
    //在刷新数据覆盖不显示数据的 cell 的分割线,如果不设置,则会显示 cell 的分割线
    UIView *footView = [UIView new];
    self.tableView.tableFooterView = footView;
    
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark -- 下拉刷新
-(void)loadDownNewDataSecond{
    //默认页数、条数
    _page = 1;
    _currentCount = 10;
    [self requestData:_currentCount page:_page];
}
#pragma mark -- 上拉加载
-(void)loadUpNewDataSecond{
    _page++;//页数
    _currentCount = 10;
    _pageIndex = _page*_currentCount;
    [self requestData:_pageIndex page:_pageIndex];
}
- (void)requestData:(NSInteger)currentCount page:(NSInteger)page{
//    NSLog(@"%@",WKCourseList_WKURL);
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setObject:@(page) forKey:@"page"];
    [data setObject:@(currentCount) forKey:@"num"];
    [WYHXNetWorkManager get:WKCourseList_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
        NSString *code = responseObj[@"code"];
        if ([code integerValue]==200) {
            NSLog(@"%@",responseObj);
            if (page==0&&self.coureseLists.count>0) {
                [self.coureseLists removeAllObjects];
                [self.tableView reloadData];
            }
            NSArray *arr = [LBCoureseListModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
            [self.coureseLists addObjectsFromArray:arr];
//            NSLog(@"%ld",self.coureseLists.count);
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.coureseLists.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LBSaomangCell *cell = [LBSaomangCell cellWithTableView:tableView];
    LBCoureseListModel *model = self.coureseLists[indexPath.section];
    cell.coursePackModel = model;
//    if ([[UserModel getUserModel].userId isEqualToString:@"38"]) {
//        cell.priceLab.hidden = YES;
//    }else{
//        cell.priceLab.hidden = NO;
//    }
    cell.freeIcon.hidden = YES;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LBCoureseListModel *model = self.coureseLists[indexPath.section];
    CourseDetailsViewController *coureseVC = [CourseDetailsViewController new];

    coureseVC.packId = model.packId;
    coureseVC.collectType = @"2";
    coureseVC.courseInfo = model.packageInfo;
    [self.navigationController pushViewController:coureseVC animated:YES];
//    [LBTabBarViewController shareInstance].selectedIndex = 2;
}

@end
